import "babel-polyfill";

//通过页面props设置create-tiltle，给页面附上标题

/*
Vue.mixin({
  created: function () {
      this.$dispatch('create-title', this.mainTitle ,this.mainSubtitle)
  }
})
*/


Vue.mixin({
    ready () {
        //ie9 select
        this.$nextTick(() => {
            setTimeout(() => {
                let select = document.querySelector("select");
                if (select) {
                    let strWidth = select.style.width;
                    select.style.width = strWidth;
                }
            }, 360)
        })
    }
})


//注册全局组件
import 'ui';

//注册全局事件
import Alert from 'ui/modules/alert.vue';
import Confirm from 'ui/modules/confirm.vue';


let vue_Alert = Vue.extend(Alert);
let vue_Confirm = Vue.extend(Confirm);

const Midway = {};

window.Midway = Midway;
window.projectName_Url = "";
window.sys_MchtId = "10000000";
window.httpProject =  process.env.NODE_ENV === 'production' ? 'http://116.62.243.178':''; // 测试环境

//加载动画
var el;
window.loading = (e) => {
    if (!el && e) {
        el = document.createElement("div");
        el.className = "popup-cover";
        el.innerHTML = `<img class="loading" src="" alt="">`;
        document.body.appendChild(el)
    } else {
        if (el) {
            document.body.removeChild(el)
            el = null;
        }
    }
}

Vue.filter('optionValue',function(value,maxLength){
    var length = value.length;
    for (var i = 0; i < value.length; i++) {
        if (value.charCodeAt(i) > 127) {
            length++;
        }
    }
    if(length > maxLength){
        return value.substr(0,maxLength)+"..."
    }else{
        return value
    }
})

Midway.confirm = function (data) {

    let el = document.createElement('div')
    document.getElementById('mounter').appendChild(el);

    let self = new vue_Confirm({
        el,
        propsData: data
    });

    self.init();

    return new Promise(function (resolve, reject) {
        self.$on("resolve", function () {
            this.hide();
            this.$destroy(true);
            resolve();
        }).$on("reject", function () {
            this.hide();
            this.$destroy(true);
            reject();
        })
    })
}

Midway.alert = function (opition) {

    let data = {}

    if (typeof(opition) == "string") {
        data.title = opition.toString();
    } else {
        data = opition
    }

    let el = document.createElement('div')
    document.getElementById('mounter').appendChild(el);

    let self = new vue_Alert({
        el,
        propsData: data
    });

    self.init();

    return new Promise(function (resolve, reject) {
        self.$on("resolve", function () {
            this.hide();
            this.$destroy(true);
            resolve();
        })
    })
}
